package com.cn.medicalcare.service.serviceimpl;

import com.cn.medicalcare.customtool.JedisUtil;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.NavigationMapper;
import com.cn.medicalcare.pojo.AllPage;
import com.cn.medicalcare.pojo.Navigation;
import com.cn.medicalcare.pojo.News;
import com.cn.medicalcare.service.NavigationService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;

import java.util.Date;
import java.util.List;

public class NavigationServiceImpl implements NavigationService {

    private static final String NAV_CACHE_KEY = "survival_navigations";

    //静态内部类单例模式
    private static class SingletonClassInstance {
        private static final NavigationServiceImpl instance = new NavigationServiceImpl();
    }

    private NavigationServiceImpl() {
    }

    public static NavigationServiceImpl getInstance() {
        return NavigationServiceImpl.SingletonClassInstance.instance;
    }


    @Override
    public int selectNavigationCount(Navigation navigation) {
        return SqlSessionFactoryTool.DoMapper(NavigationMapper.class, m -> m.selectNavigationCount(navigation));
    }

    @Override
    public List<Navigation> selectNavigationInPage(AllPage<Navigation> navigationAllPage) {
        List<Navigation> list = SqlSessionFactoryTool.DoMapper(NavigationMapper.class, m -> m.selectNavigationInPage(navigationAllPage));
        return list;
    }

    @Override
    public boolean insertNavigation(Navigation navigation) {
        navigation.setSurvivalupdatetime(new Date());
        boolean insertFlag = SqlSessionFactoryTool.DoMapperAndCommit(NavigationMapper.class, m -> m.insertNavigation(navigation));
        if (insertFlag) {
            JedisUtil.jedisDo(j -> j.del(NAV_CACHE_KEY));
        }
        return insertFlag;
    }

    @Override
    public boolean updateNavigation(Navigation navigation) {
        if (navigation.getSurvival() == 1) {
            navigation.setSurvivalupdatetime(new Date());
        }
        boolean updateFlag = SqlSessionFactoryTool.DoMapperAndCommit(NavigationMapper.class, m -> m.updateNavigation(navigation));
        if (updateFlag) {
            JedisUtil.jedisDo(j -> j.del(NAV_CACHE_KEY));
        }
        return updateFlag;
    }

    @Override
    public List<Navigation> selectNavigation() {
        List<Navigation> list = null;
        try {
            String navjson = JedisUtil.jedisDo(j -> j.get(NAV_CACHE_KEY));
            if (StringUtils.isNotEmpty(navjson)) {
                list = JsonTool.toCollection(navjson, new TypeReference<List<Navigation>>() {
                });
                JedisUtil.jedisDo(j -> j.expire(NAV_CACHE_KEY, JedisUtil.CACHE_TIME));
            }
        } catch (Exception e) {
        }
        if (list == null) {
            list = SqlSessionFactoryTool.DoMapper(NavigationMapper.class, m -> m.selectSurvivalNavigation());
            try {
                String navjson = JsonTool.toJson(list);
                JedisUtil.jedisDo(j -> j.setex(NAV_CACHE_KEY, JedisUtil.CACHE_TIME, navjson));
            } catch (Exception e) {
            }
        }
        return list;
    }
}

